<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="90px"
    >
      <el-form-item label="项目编号" prop="vProjectNo">
        <el-input
          v-model="queryParams.vProjectNo"
          placeholder="请输入项目编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="请购单编号" prop="vBuyNo">
        <el-input
          v-model="queryParams.vBuyNo"
          placeholder="请输入请购单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="零件名称" prop="vSpaName">
        <el-input
          v-model="queryParams.vSpaName"
          placeholder="请输入零件名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="规格型号" prop="vSpaModel">
        <el-input
          v-model="queryParams.vSpaName"
          placeholder="请输入规格型号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="品牌" prop="vSpaBrand">
        <el-input
          v-model="queryParams.vSpaBrand"
          placeholder="请输入品牌"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" clearable>
          <el-option
            v-for="dict in dict.type.buy_requestitem_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          size="mini"
          @click="handleToInv"
          :disabled="multiple"
          >提交库房验证库存</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          size="mini"
          icon="el-icon-plus"
          @click="handleToBill"
          :disabled="multiple"
          >新建合同</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          size="mini"
          icon="el-icon-plus"
          @click="updateToBill"
          :disabled="multiple"
          >添加到已有合同</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="itemList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />

      <el-table-column label="请购单编号" align="center" prop="vBuyNo" />
      <el-table-column label="项目编号" align="center" prop="vProjectNo" />
      <el-table-column label="项目描述" align="center" prop="vProjectDec" />
      <el-table-column label="零件名称" align="center" prop="vSpaName" />
      <el-table-column label="规格型号" align="center" prop="vSpaModel" />
      <el-table-column label="品牌" align="center" prop="vSpaBrand" />
      <el-table-column label="单位" align="center" prop="vUnit" />
      <el-table-column label="数量" align="center" prop="nQty" />
      <el-table-column label="库存已有数量" align="center" prop="nInvQty" />
      <el-table-column label="待购数量" align="center" prop="nBuyQty" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.buy_requestitem_status"
            :value="scope.row.status"
          />
        </template>
      </el-table-column>
      <el-table-column label="质保期" align="center" prop="nDays" />
      <el-table-column label="备注" align="center" prop="remark" />
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改【外购合同】对话框 -->
    <el-drawer
      :title="title"
      :before-close="handleClose"
      :visible.sync="open"
      size="70%"
      ref="drawer"
    >
      <div class="drawer-container">
        <div class="xin-main">
          <div class="xin-content" style="height: 90%">
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
              <el-row>
                <el-col :span="8">
                  <el-form-item label="合同编号" prop="vBillNo">
                    <el-input
                      v-model="form.vBillNo"
                      placeholder="请输入合同编号"
                    />
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="8">
                  <el-form-item label="品牌" prop="vBrand">
                    <el-input v-model="form.vBrand" placeholder="请输入品牌" />
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="供应商" prop="supplier">
                    <el-input
                      v-model="form.supplier"
                      placeholder="请输入供应商"
                    />
                  </el-form-item>
                </el-col>

                <el-col :span="8">
                  <el-form-item label="货期" prop="nDays">
                    <el-input v-model="form.nDays" placeholder="请输入货期" />
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="8">
                  <el-form-item label="申请人" prop="applyBy">
                    <el-input
                      v-model="form.applyBy"
                      placeholder="请输入申请人"
                    />
                  </el-form-item>
                </el-col>

                <el-col :span="8">
                  <el-form-item label="备注" prop="remark">
                    <el-input v-model="form.remark" placeholder="请输入备注" />
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="合同用印日期" prop="contractTime">
                    <el-date-picker
                      v-model="form.contractTime"
                      type="date"
                      value-format="yyyy-MM-dd"
                      placeholder="选择日期"
                    >
                    </el-date-picker>
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form>
            <el-divider content-position="left"></el-divider>

            <el-table :data="contractItems" height="450px" :key="refreshTable">
              <el-table-column
                label="序号"
                align="center"
                type="index"
                prop="index"
              />
              <el-table-column
                label="请购单编号"
                align="center"
                prop="vBuyNo"
              />
              <el-table-column
                label="项目号"
                align="center"
                prop="vProjectNo"
              />
              <el-table-column
                label="项目名称"
                align="center"
                prop="vProjectDec"
              />
              <el-table-column label="申请人" align="center" prop="applyBy" />
              <el-table-column
                label="物料名称"
                align="center"
                prop="vSpaName"
              />
              <el-table-column
                label="规格型号"
                align="center"
                prop="vSpaModel"
                width="200px"
              />
              <el-table-column label="品牌" align="center" prop="vSpaBrand" />
              <!-- <el-table-column label="单位" align="center" prop="vUnit" /> -->
              <el-table-column label="单价" align="center" prop="nPrice">
                <template slot-scope="scope">
                  <el-input size="small" v-model="scope.row.nPrice"></el-input>
                </template>
              </el-table-column>
              <el-table-column label="数量" align="center" prop="nQty" />
              <el-table-column label="供应商" align="center" prop="supplier">
              </el-table-column>
              <el-table-column label="货期" align="center" prop="nDays" />
              <el-table-column label="备注" align="center" prop="remark" />
            </el-table>
            <div
              class="xin-footer"
              style="position: fixed; bottom: 5px; right: 10px"
            >
              <el-button type="primary" @click="submitForm">确 定</el-button>
              <el-button @click="cancel('02')">取 消</el-button>
            </div>
          </div>
        </div>
      </div>
    </el-drawer>

    <el-dialog
      :title="updateTitle"
      :visible.sync="updateOpen"
      width="500px"
      append-to-body
    >
      <el-form
        ref="updateForm"
        :form="updateForm"
        label-width="120px"
        :rules="rules"
      >
        <el-form-item label="合同编号" prop="vBillNo">
          <el-select
            v-model="updateForm.vBillNo"
            placeholder="合同编号"
            clearable
            filterable
          >
            <el-option
              v-for="dict in contracts"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="turnToUpdate">确 定</el-button>
        <el-button @click="cancelToUpdate">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  list,
  handleToInv,
  insertBuyPurchaseBill,
  getContracts,
  getContractInfo,
  updateBuyPurchaseBill,
} from "@/api/buy/buy0009";

export default {
  name: "Item",
  dicts: ["buy_requestitem_status"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 请购单明细表格数据
      itemList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        vSpaName: null,
        vSpaModel: null,
        vSpaBrand: null,
        vUnit: null,
        nQty: null,
        nPrice: null,
        status: null,
        nDays: null,
        vProjectNo: null,
        vProjectDec: null,
      },
      // 表单参数
      // 表单参数
      form: {
        id: null,
        vBillNo: null,
        status: null,
        vProjectNo: null,
        vProjectDec: null,
        delFlag: null,
        applyBy: null,
        oprBy: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        supplier: null,
        nAmt: null,
        nDays: null,
        contractTime: null,
        vBrand: null,
      },
      updateForm: {
        vBillNo: null,
      },
      // 表单校验
      rules: {
        vBillNo: {
          required: true,
          trigger: "blur",
          message: "合同编号不能为空",
        },
        vBrand: {
          required: true,
          trigger: "blur",
          message: "品牌不能为空",
        },
        supplier: {
          required: true,
          trigger: "blur",
          message: "供应商不能为空",
        },
        nDays: {
          required: true,
          trigger: "blur",
          message: "货期不能为空",
        },
        contractTime: {
          required: true,
          trigger: "blur",
          message: "合同用印日期不能为空",
        },
      },
      updateRules: {
        vBillNo: {
          required: true,
          trigger: "blur",
          message: "合同编号不能为空",
        },
      },
      selectionItems: [],
      contractItems: [],
      refreshTable: false,
      updateOpen: false,
      updateTitle: "",
      contracts: [],
    };
  },
  watch: {
    "form.supplier": function (newVal, oldVal) {
      if (newVal) {
        this.contractItems.map((item, index) => {
          item.supplier = newVal;
        });
        this.refreshTable = !this.refreshTable;
      }
    },
    "form.nDays": function (newVal, oldVal) {
      if (newVal) {
        this.contractItems.map((item, index) => {
          item.nDays = newVal;
        });
        this.refreshTable = !this.refreshTable;
      }
    },
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询请购单明细列表 */
    getList() {
      this.loading = true;
      console.log(this.queryParams);
      list(this.queryParams).then((response) => {
        this.itemList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        vBillNo: null,
        status: null,
        vProjectNo: null,
        vProjectDec: null,
        delFlag: null,
        applyBy: null,
        oprBy: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        supplier: null,
        nAmt: null,
        nDays: null,
        contractTime: null,
        vBrand: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
      this.selectionItems = selection;
    },
    handleToInv() {
      handleToInv(this.selectionItems).then((response) => {
        this.$modal.msgSuccess("操作成功");
        this.getList();
      });
    },
    handleToBill() {
      this.title = "新建合同";
      this.selectionItems.map((item) => {
        item.supplier = "";
      });
      this.contractItems = Object.assign([], this.selectionItems);
      this.contractItems.map((item) => {
        item.nQty = item.nBuyQty;
      });
      this.open = true;
      this.reset();
    },
    handleClose() {
      this.open = false;
      this.getList();
    },
    updateToBill() {
      this.selectionItems.map((item) => {
        item.supplier = "";
      });
      this.contractItems = Object.assign([], this.selectionItems);
      this.contractItems.map((item) => {
        item.nQty = item.nBuyQty;
      });
      this.updateOpen = true;
      this.updateTitle = "选择已有合同";

      getContracts().then((response) => {
        this.contracts = response.data;
      });
      this.updateForm.vBillNo = null;
    },
    turnToUpdate() {
      getContractInfo(this.updateForm.vBillNo).then((response) => {
        this.form = response.data.billPO;
        this.contractItems.map((item) => {
          item.requestItemId = item.id;
          item.id = null;
          item.supplier = response.data.billPO.supplier;
          item.nDays = response.data.billPO.nDays;
        });
        this.contractItems = [
          ...this.contractItems,
          ...response.data.itemPOList,
        ];
        this.open = true;
        this.title = "添加合同明细";
        this.updateOpen = false;
      });
    },
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          let para = {
            billPO: this.form,
            itemPOList: this.contractItems,
          };
          if (this.form.id != undefined) {
            updateBuyPurchaseBill(para).then((response) => {
              this.$modal.msgSuccess("操作成功");
              this.open = false;
              this.getList();
            });
          } else {
            insertBuyPurchaseBill(para).then((response) => {
              this.$modal.msgSuccess("操作成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
  },
};
</script>
